package lvbu.wang.lvbuoperator.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qing.library.utils.IntentUtils;
import com.qing.library.utils.L;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lvbu.wang.lvbuoperator.R;
import lvbu.wang.lvbuoperator.adapter.SaleProductAdapter;
import lvbu.wang.lvbuoperator.bean.JsonSaleProduct;
import lvbu.wang.lvbuoperator.constants.ConstantsValue;
import lvbu.wang.lvbuoperator.constants.RouteConstant;
import lvbu.wang.lvbuoperator.core.BaseActivity;
import lvbu.wang.lvbuoperator.function.nologin.NoLoginOperationBluetoothListActivity;
import lvbu.wang.lvbuoperator.http.IotHttpHelp;
import lvbu.wang.lvbuoperator.manager.ThreadPool;
import lvbu.wang.lvbuoperator.ui.debug.BleLocalDebugActivity;
import lvbu.wang.lvbuoperator.ui.index.BleIndexActivity;
import lvbu.wang.lvbuoperator.ui.index.IotIndexActivity;
import lvbu.wang.lvbuoperator.ui.scan.BleBluetoothListActivity;

/**
 * Created by DaiQing.Zha on 2017/3/18 0018.
 */
public class ProductSelectActivity extends BaseActivity implements View.OnClickListener{

    private ExpandableListView mListViewProductList;
    private List<JsonSaleProduct.ProductCategoryDict> productCategoryDictList;
    private List<List<JsonSaleProduct.ProductInfo>> productInfoList;
    private SaleProductAdapter saleProductAdapter;
    private Button mBtnLocalDebug;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_select);
    }

    @Override
    protected void init() {
        getSupportActionBar().setTitle(getString(R.string.xml_productSelect_title));
        mListViewProductList = (ExpandableListView) findViewById(R.id.eList_productList);
        mBtnLocalDebug = (Button) findViewById(R.id.btn_localDebug);
        productCategoryDictList = new ArrayList<>();
        productInfoList = new ArrayList<>();
        saleProductAdapter = new SaleProductAdapter(ProductSelectActivity.this,productCategoryDictList,productInfoList);
        mListViewProductList.setAdapter(saleProductAdapter);
        mBtnLocalDebug.setOnClickListener(this);
        ThreadPool.getInstance().addTask(new Runnable() {
            @Override
            public void run() {
                Map<String, Object> queryStrings = new HashMap<>();
                String source = IotHttpHelp.getData(RouteConstant.GET_LIST_JSON_SALE_PRODUCT_PRODUCT_CATE_GORY_DICT, queryStrings, true, ProductSelectActivity.this);
                if (IotHttpHelp.checkSource(source)){
                    List<JsonSaleProduct.ProductCategoryDict> list = new Gson().fromJson(source, new TypeToken<List<JsonSaleProduct.ProductCategoryDict>>() {
                    }.getType());
                    productCategoryDictList.addAll(list);
                    for (int i = 0; i < list.size(); i ++){
                        productInfoList.add(new ArrayList<JsonSaleProduct.ProductInfo>());

                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            saleProductAdapter.notifyDataSetChanged();
                        }
                    });
                }else{
                    L.e("productTE","source = " + source);
                }
            }
        });
        mListViewProductList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, final int groupPosition, long id) {
                ThreadPool.getInstance().addTask(new Runnable() {
                    @Override
                    public void run() {
                        Map<String, Object> queryStrings = new HashMap<>();
                        queryStrings.put("categoryCode",productCategoryDictList.get(groupPosition).getCategoryCode());
                        final String source = IotHttpHelp.getData(RouteConstant.GET_LIST_JSON_SALE_PRODUCT_PRODUCT_INFO_BY_CATEGORY_CODE, queryStrings, true, ProductSelectActivity.this);
                        if (IotHttpHelp.checkSource(source)){
                            List<JsonSaleProduct.ProductInfo> list = new Gson().fromJson(source, new TypeToken<List<JsonSaleProduct.ProductInfo>>() {
                            }.getType());
                            productInfoList.set(groupPosition,list);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    saleProductAdapter.notifyDataSetChanged();
                                }
                            });
                        }else{
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    showToastMsg(IotHttpHelp.source2Message(ProductSelectActivity.this,source));
                                }
                            });
                        }
                    }
                });
                return false;
            }
        });
        mListViewProductList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                String categoryCode = productCategoryDictList.get(groupPosition).getCategoryCode();
                String productId = productInfoList.get(groupPosition).get(childPosition).getProductId();
                String productName = productInfoList.get(groupPosition).get(childPosition).getProductName();
                if (categoryCode != null){
                    if (groupPosition < 2){
                        if (categoryCode.toLowerCase().contains("iot")){
                            ConstantsValue.MOTOR_TYPE = ConstantsValue.MOTOR_TYPE_IOT_OPERATION;
                            Intent intent = new Intent(ProductSelectActivity.this,IotIndexActivity.class);
                            IntentUtils.startActivity(ProductSelectActivity.this,intent);
                        }else if (categoryCode.toLowerCase().contains("bt")){
                            ConstantsValue.MOTOR_TYPE = ConstantsValue.MOTOR_TYPE_BLE_OPERATION;
                            Intent intent = new Intent(ProductSelectActivity.this,BleIndexActivity.class);
                            IntentUtils.startActivity(ProductSelectActivity.this,intent);
                        }
                    }else{
                        showToastMsg("暂时无法使用");
                    }
                    BaseActivity.setBluetoothProductId(ProductSelectActivity.this,productId);
                    BaseActivity.setBluetoothProductName(ProductSelectActivity.this,productName);
                    BaseActivity.setMotorType(ProductSelectActivity.this,ConstantsValue.MOTOR_TYPE);
                }
                return false;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_localDebug:
                Intent intent = new Intent(ProductSelectActivity.this,BleBluetoothListActivity.class);
                intent.putExtra(ConstantsValue.OPERATION_TYPE,ConstantsValue.OPERATION_TYPE_LOCAL_DEBUG);
                BaseActivity.setMotorType(this,ConstantsValue.MOTOR_TYPE_BLE_OPERATION);
                startActivity(intent);
                break;
        }
    }
}
